package controllers

import (
	models "demo/webSocket/modesl"
	"demo/webSocket/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
)

//ws连接
func Connect(c *gin.Context) {

	if err := service.NewWebSocketService().GetWSocket().HandleRequest(c.Writer, c.Request); err != nil {
		fmt.Println("ws连接失败：", err.Error())
		return
	}

}

// TestWsSendOneUser 推送给单个用户,注意要指定用户的token
func TestWsSendOneUser(c *gin.Context) {
	err := service.NewWebSocketService().BoardCastOneUser("59f0ff222ac4f3e06be32330c5b2ba9d25", models.WsMessage{
		ID:     "1",
		Model:  models.WsModelAdmin,
		Action: models.WsActionSort,
		Data:   "指定的单个用户",
	})
	if err != nil {
		log.Fatalln("发送失败：")
	}
}

// TestWsSendDesignateUser 推送给指定多个用户，注意要指定多个用户的token
func TestWsSendDesignateUser(c *gin.Context) {
	// 推送指定的多个用户
	err := service.NewWebSocketService().BoardCastDesignateUser([]string{"59f0ff222ac4f3e06be32330c5b2ba9d", "59f0ff222ac4f3e06be32330c5b2ba9d2"}, models.WsMessage{
		ID:     "2",
		Model:  "2",
		Action: "2",
		Data:   "指定的多个用户",
		Other:  nil,
		Dialog: 0,
	})
	if err != nil {
		log.Fatalln("发送失败：")
		return
	}
}

// TestSendAllUser 推送给所有用户。
func TestSendAllUser(c *gin.Context) {
	err := service.NewWebSocketService().BoardCastAllUser(models.WsMessage{
		ID:     "1",
		Model:  models.WsModelAdmin,
		Action: models.WsActionSort,
		Data:   "所有用户",
	})
	if err != nil {
		log.Fatalln("发送失败：")
	}
}

func TestClose(c *gin.Context) {
	var (
		params struct {
			RemoteAddress string `json:"remote_address"`
		}
		//	err error
	)

	service.NewWebSocketService().DeleteClient(params.RemoteAddress)
}
